package com.dao;

import com.bean.Student;
import com.view.Main;

import java.util.ArrayList;
import java.util.Scanner;

public class Dao {

  public static ArrayList<Student> arr = new ArrayList<>();
  public static Scanner sc = new Scanner(System.in);

  public static void add(ArrayList<Student> arr) {
    String id;
    while (true) {
      System.out.println("请输入你要添加的学号");
      id = sc.next();
      boolean flag = false;
      for (int i = 0; i < arr.size(); i++) {
        Student s = arr.get(i);
        if (s.getStuid().equals(id)) {
          flag = true;
          break;
        }
      }
      if (flag == true) {
        System.out.println("你输入的学号已存在，请重新输入");
      } else {
        break;
      }
    }
    System.out.println("输入学号有效，请继续输入姓名：");
    String name = sc.next();
    if (name != null) {
      System.out.println("请输入性别：");
      String sex = sc.next();
      if (sex != null) {
        System.out.println("请输入年龄：");
        int age = sc.nextInt();
        if (age > 0) {
          System.out.println("请输入地址：");
          String address = sc.next();
          if (address != null) {
            Student s = new Student();
            s.setStuid(id);
            s.setName(name);
            s.setAge(age);
            s.setSex(sex);
            s.setAddress(address);
            arr.add(s);
            System.out.println("添加成功！！");
            actionLast();
            return;
          } else {
            System.out.println("添加失败");
          }
        } else {
          System.out.println("添加失败");
        }
      } else {
        System.out.println("添加失败");
      }
    } else {
      System.out.println("添加失败");
    }
  }

  public static void delete(ArrayList<Student> arr) {
    while (true) {
      System.out.println("请输入要删除的学号");
      String deid = sc.next();
      int index = -1;
      for (int i = 0; i < arr.size(); i++) {
        Student s = arr.get(i);
        if (s.getStuid().equals(deid)) {
          index = i;
          break;
        }
        if (index == -1) {
          System.out.println("你要删除的学生不存在，请重新选择！");
        } else {
          arr.remove(index);
          System.out.println("删除学生成功！！");
          actionLast();
          return;
        }
      }
    }
  }

  public static void update(ArrayList<Student> arr) {
    while (true) {
      System.out.println("请输入要更改的学号");
      String updid = sc.next();
      int index = -1;
      for (int i = 0; i < arr.size(); i++) {
        Student s = arr.get(i);
        if (s.getStuid().equals(updid)) {
          index = i;
        }
      }
      if (index == -1) {
        System.out.println("你要修改的学生不存在，请重新输入！");
      } else {
        System.out.println("输入学号有效，请继续输入姓名：");
        String name = sc.next();
        if (name != null) {
          System.out.println("请输入性别：");
          String sex = sc.next();
          if (sex != null) {
            System.out.println("请输入年龄：");
            int age = sc.nextInt();
            if (age > 0) {
              System.out.println("请输入地址：");
              String address = sc.next();
              if (address != null) {
                Student s = new Student();
                s.setStuid(updid);
                s.setName(name);
                s.setAge(age);
                s.setSex(sex);
                s.setAddress(address);
                arr.set(index, s);
                System.out.println("更改成功！！");
                actionLast();
                return;
              } else {
                System.out.println("更改失败");
              }
            } else {
              System.out.println("更改失败");
            }
          } else {
            System.out.println("更改失败");
          }
        } else {
          System.out.println("更改失败");
        }
      }
    }
  }

  public static void findAll(ArrayList<Student> arr) {
    if (arr.size() == 0) {
      System.out.println("不好意思，目前还没有学生的信息可供查询，请返回重新操作！");
    } else {
      System.out.println("学号\t姓名\t性别\t年龄\t居住地");
      for (int i = 0; i < arr.size(); i++) {
        Student s = arr.get(i);
        System.out.println(s.toString());
      }
    }
    actionLast();
  }

  public static void find(ArrayList<Student> arr) {
    if (arr.size() == 0) {
      System.out.println("不好意思，目前还没有学生的信息可供查询，请返回重新操作！");
    } else {
      while (true) {
        System.out.println("请输入要查询的学号");
        String fid = sc.next();
        int index = -1;
        for (int i = 0; i < arr.size(); i++) {
          Student s = arr.get(i);
          if (s.getStuid().equals(fid)) {
            index = i;
            }
          }
          if (index == -1) {
            System.out.println("你要查询的学生不存在，请重新输入！");
          } else {
            System.out.println("学号\t姓名\t性别\t年龄\t居住地");
            arr.get(index).toString();
          }
        }
      }
    actionLast();
    return;
    }

  public static void exit() {
    System.out.println("谢谢你的使用,学生管理系统为你服务！");
    // 退出JVM
    System.exit(0);
  }

  public static void actionLast() {
    while (true) {
      System.out.println("是否进行下一步操作,请输入1/2：");
      int a = sc.nextInt();
      if (a == 1) {
        Main m = new Main();
        m.action();
        return;
      } else if (a == 2) {
        exit();
        return;
      } else {
        System.out.println("输入错误，请重新输入：");
        break;
      }
    }
  }
}
